/**
 * qrImg
 */
(function (factory) {
    if (typeof define === 'function' && define.amd) {
        // AMD
        define(['jquery'], factory);
    } else if (typeof exports === 'object') {
        // CommonJS
        factory(require('jquery'));
    } else {
        // Browser globals
        factory(jQuery);
    }
}(function ($) {
    $.fn.nov.qrCode = function (options) {
        var $this = this;
        $this.version = '1.0';

        var settings = {
            threshold: 0,
            failure_limit: 0,
            event: "scroll",
            effect: "show",
            container: window,
            data_attribute: "nov-qrcode",
            skip_invisible: false,
            appear: null,
            load: null,
            placeholder: ""
        };

        $.extend($this.settings, options);


        var bid = $.nov.cookie("nov.bid");
        console.log(bid);
        if (!bid) {
            $.nov.finger.get(function (result) {
                $.nov.cookie("nov.bid", result);
                console.log(result);
            });
        }

        if ($this.length > 0) {
            this.each(function () {
                var $self = $(this);

                if ($self.attr("src") === undefined || $self.attr("src") === false) {
                    if ($self.is("img")) {
                        $self.attr("src", settings.placeholder);
                    }
                }

                $self.one("load", function () {
                    var original = $self.attr("data-" + settings.data_attribute);
                    $self.hide();
                    if ($self.is("img")) {
                        $self.attr("src", original);
                    } else {
                        $self.css("background-image", "url('" + original + "')");
                    }
                    $self[settings.effect](settings.effect_speed);

                    self.loaded = true;

                    /* Remove image from array so it is not looped next time. */
                    var temp = $.grep(elements, function (element) {
                        return !element.loaded;
                    });
                    elements = $(temp);

                    if (settings.load) {
                        var elements_left = elements.length;
                        settings.load.call(self, elements_left, settings);
                    }
                }).attr("src", $self.attr("data-" + settings.data_attribute));
            });
        }



        return {};

    };
}));

//$(document).ready(function () {
//    $("img [data-nov-qrcode]").nov.qrCode();
//});
